using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using SLProxy;
using libsecondlife;
using libsecondlife.Packets;
using System.Net;

namespace StickyChat
{
    public class StickyFilters : UserControl, StickIn
    {
        private StickyServices loader;
        private string myName;
        private CheckBox FilterScreenshots;
        private CheckBox InstaSit;
        private GroupBox TeleportLuresGroup;
        private RadioButton TPdecline;
        private RadioButton TPaccept;
        private RadioButton TPask;
        private GroupBox InventoryOffersGroup;
        private RadioButton INVdecline;
        private RadioButton INVaccept;
        private RadioButton INVask;
        private GroupBox FriendshipOffersGroup;
        private RadioButton FRdecline;
        private RadioButton FRaccept;
        private RadioButton FRask;
        private CheckBox FilterTyping;

        public StickyFilters(StickyServices p)
        {
            loader = p;
            myName = "Filters";
            InitializeComponent();
        }

        #region StickIn Members
        public string name
        {
            get { return myName; }
        }

        public void start()
        {
            loader.proxy.AddDelegate(PacketType.ChatFromSimulator, Direction.Incoming, ChatFromSimulator);
            loader.proxy.AddDelegate(PacketType.AvatarAnimation, Direction.Incoming, AvatarAnimation);
            loader.proxy.AddDelegate(PacketType.AgentAnimation, Direction.Outgoing, AgentAnimation);
            loader.proxy.AddDelegate(PacketType.SoundTrigger, Direction.Incoming, SoundTriggerIn);
            loader.proxy.AddDelegate(PacketType.SoundTrigger, Direction.Outgoing, SoundTriggerOut);
            loader.proxy.AddDelegate(PacketType.AvatarSitResponse, Direction.Incoming, AvatarSitResponse);
            loader.proxy.AddDelegate(PacketType.ImprovedInstantMessage, Direction.Incoming, ImprovedInstantMessage);
        }

        public void stop()
        {
            loader.proxy.RemoveDelegate(PacketType.ChatFromSimulator, Direction.Incoming, ChatFromSimulator);
            loader.proxy.RemoveDelegate(PacketType.AvatarAnimation, Direction.Incoming, AvatarAnimation);
            loader.proxy.RemoveDelegate(PacketType.AgentAnimation, Direction.Outgoing, AgentAnimation);
            loader.proxy.RemoveDelegate(PacketType.SoundTrigger, Direction.Incoming, SoundTriggerIn);
            loader.proxy.RemoveDelegate(PacketType.SoundTrigger, Direction.Outgoing, SoundTriggerOut);
            loader.proxy.RemoveDelegate(PacketType.AvatarSitResponse, Direction.Incoming, AvatarSitResponse);
            loader.proxy.RemoveDelegate(PacketType.ImprovedInstantMessage, Direction.Incoming, ImprovedInstantMessage);
        }
        #endregion StickIn Members

        #region Delegates
        private Packet ChatFromSimulator(Packet packet, IPEndPoint sim)
        {
            // deconstruct the packet
            ChatFromSimulatorPacket chat = (ChatFromSimulatorPacket)packet;

            if (FilterTyping.Checked)
            {
                if (chat.ChatData.ChatType == 4)
                {
                    Console.WriteLine(Helpers.FieldToUTF8String(chat.ChatData.FromName) + " has started typing");
                    return null;
                }
                else if (chat.ChatData.ChatType == 5)
                {
                    Console.WriteLine(Helpers.FieldToUTF8String(chat.ChatData.FromName) + " has stopped typing");
                    return null;
                }
            }
            return chat;
        }

        private Packet AvatarAnimation(Packet packet, IPEndPoint sim)
        {
            // deconstruct the packet
            AvatarAnimationPacket anim = (AvatarAnimationPacket)packet;
            List<string> filteredKeys = new List<string>();
            if (FilterTyping.Checked)
                filteredKeys.Add("c541c47fe0c0058bad1ad6ae3a4584d9");
            if (FilterScreenshots.Checked)
                filteredKeys.Add("eae8905b271a99e24c0e31106afd100c");

            if (filteredKeys.Count > 0)
            {
                for (int i = 0; i < anim.AnimationList.Length; i++)
                {
                    if (filteredKeys.Contains(anim.AnimationList[i].AnimID.ToString()))
                    {
                        anim.AnimationList[i].AnimID = LLUUID.Zero;
                    }
                }
            }
            return anim;
        }

        private Packet AgentAnimation(Packet packet, IPEndPoint sim)
        {
            if (FilterScreenshots.Checked)
            {
                // deconstruct the packet
                AgentAnimationPacket anim = (AgentAnimationPacket)packet;

                for (int i = 0; i < anim.AnimationList.Length; i++)
                {
                    if (anim.AnimationList[i].StartAnim
                        && anim.AnimationList[i].AnimID.ToString() == "eae8905b271a99e24c0e31106afd100c")
                    {
                        return null;
                    }
                }
            }
            return packet;
        }

        private Packet SoundTriggerIn(Packet packet, IPEndPoint sim)
        {
            if (FilterScreenshots.Checked)
            {
                // deconstruct the packet
                SoundTriggerPacket trigger = (SoundTriggerPacket)packet;

                if (trigger.SoundData.SoundID.ToString() == "3d09f5823851c0e0f5ba277ac5c73fb4")
                {
                    return null;
                }
            }
            return packet;
        }

        private Packet SoundTriggerOut(Packet packet, IPEndPoint sim)
        {
            if (FilterScreenshots.Checked)
            {
                // deconstruct the packet
                SoundTriggerPacket trigger = (SoundTriggerPacket)packet;

                if (trigger.SoundData.SoundID.ToString() == "3d09f5823851c0e0f5ba277ac5c73fb4")
                {
                    return null;
                }
            }
            return packet;
        }

        private Packet AvatarSitResponse(Packet packet, IPEndPoint sim)
        {
            if (InstaSit.Checked)
            {
                AgentSitPacket sit = new AgentSitPacket();
                sit.AgentData.AgentID = loader.AgentID;
                sit.AgentData.SessionID = loader.SessionID;
                loader.proxy.InjectPacket(sit, Direction.Outgoing);
                return null;
            }
            return packet;
        }

        private Packet ImprovedInstantMessage(Packet packet, IPEndPoint sim)
        {
            ImprovedInstantMessagePacket im = (ImprovedInstantMessagePacket)packet;
            if (!TPask.Checked || !INVask.Checked || !FRask.Checked)
            {
                switch (im.MessageBlock.Dialog)
                {
                    case 4: // INVENTORY_OFFERED
                    case 9: // TASK_INVENTORY_OFFERED
                        if (!INVask.Checked)
                        {
                            ImprovedInstantMessagePacket inject = new ImprovedInstantMessagePacket();
                            inject.AgentData.AgentID = loader.AgentID;
                            inject.AgentData.SessionID = loader.SessionID;
                            inject.MessageBlock.FromGroup = false;
                            inject.MessageBlock.ToAgentID = im.AgentData.AgentID;
                            inject.MessageBlock.Offline = 0; // ONLINE
                            inject.MessageBlock.ID = im.MessageBlock.ID;
                            inject.MessageBlock.Timestamp = 0; // no timestamp necessary
                            inject.MessageBlock.FromAgentName = Helpers.StringToField("StickyChat!");
                            inject.MessageBlock.Message = Helpers.StringToField("");
                            inject.MessageBlock.ParentEstateID = 0;
                            inject.MessageBlock.RegionID = LLUUID.Zero;
                            inject.MessageBlock.Position = LLVector3.Zero;
                            inject.MessageBlock.BinaryBucket = new byte[0];
                            if (INVaccept.Checked)
                            {
                                inject.MessageBlock.Dialog = (byte)(im.MessageBlock.Dialog + 1);
                            }
                            else
                            {
                                inject.MessageBlock.Dialog = (byte)(im.MessageBlock.Dialog + 2);
                            }
                            Console.WriteLine("Inject:");
                            Console.WriteLine(inject.ToString());
                            loader.proxy.InjectPacket(inject, Direction.Outgoing);
                            return null;
                        }
                        break;
                    case 22: // LURE_USER
                        if (!TPask.Checked)
                        {
                            if (TPaccept.Checked)
                            {
                                im.MessageBlock.Dialog = 25; // GODLIKE_LURE_USER :D
                                return im;
                            }
                            else
                            {
                                ImprovedInstantMessagePacket inject = new ImprovedInstantMessagePacket();
                                inject.AgentData.AgentID = loader.AgentID;
                                inject.AgentData.SessionID = loader.SessionID;
                                inject.MessageBlock.FromGroup = false;
                                inject.MessageBlock.ToAgentID = im.AgentData.AgentID;
                                inject.MessageBlock.Offline = 0; // ONLINE
                                inject.MessageBlock.ID = im.MessageBlock.ID;
                                inject.MessageBlock.Timestamp = 0; // no timestamp necessary
                                inject.MessageBlock.FromAgentName = Helpers.StringToField("StickyChat!");
                                inject.MessageBlock.Message = Helpers.StringToField("");
                                inject.MessageBlock.ParentEstateID = 0;
                                inject.MessageBlock.RegionID = LLUUID.Zero;
                                inject.MessageBlock.Position = LLVector3.Zero;
                                inject.MessageBlock.BinaryBucket = new byte[0];
                                inject.MessageBlock.Dialog = (byte)(24);
                                loader.proxy.InjectPacket(inject, Direction.Outgoing);
                                Console.WriteLine("Rejected teleport offer from " + Helpers.FieldToUTF8String(im.MessageBlock.FromAgentName));
                                return null;
                            }
                        }
                        break;
                    case 38: // FRIENDSHIP_OFFERED
                        if (FRaccept.Checked)
                        {
                            AcceptFriendshipPacket inject = new AcceptFriendshipPacket();
                            inject.AgentData.AgentID = loader.AgentID;
                            inject.AgentData.SessionID = loader.SessionID;
                            inject.FolderData = new AcceptFriendshipPacket.FolderDataBlock[1];
                            inject.FolderData[0] = new AcceptFriendshipPacket.FolderDataBlock();
                            inject.FolderData[0].FolderID = LLUUID.Zero; // need inventory folder key?
                            inject.TransactionBlock.TransactionID = im.MessageBlock.ID;
                            loader.proxy.InjectPacket(inject, Direction.Outgoing);
                            // can transform to an accept packet to trick the client that they accepted our offer
                            // this will let them show up on our friends list, but produces a dialog box
                            //im.MessageBlock.Dialog = (byte)39;
                            //return im;
                            loader.SayToUser(Helpers.FieldToUTF8String(im.MessageBlock.FromAgentName), "/me will be added next time you log-in");
                            return null;
                        }
                        else if (FRdecline.Checked)
                        {
                            DeclineFriendshipPacket inject = new DeclineFriendshipPacket();
                            inject.AgentData.AgentID = loader.AgentID;
                            inject.AgentData.SessionID = loader.SessionID;
                            inject.TransactionBlock.TransactionID = im.MessageBlock.ID;
                            loader.proxy.InjectPacket(inject, Direction.Outgoing);
                            return null;
                        }
                        break;
                }
            }
            return packet;
        }
        #endregion Delegates

        private void InitializeComponent()
        {
            this.FilterScreenshots = new System.Windows.Forms.CheckBox();
            this.FilterTyping = new System.Windows.Forms.CheckBox();
            this.InstaSit = new System.Windows.Forms.CheckBox();
            this.TeleportLuresGroup = new System.Windows.Forms.GroupBox();
            this.TPdecline = new System.Windows.Forms.RadioButton();
            this.TPaccept = new System.Windows.Forms.RadioButton();
            this.TPask = new System.Windows.Forms.RadioButton();
            this.InventoryOffersGroup = new System.Windows.Forms.GroupBox();
            this.INVdecline = new System.Windows.Forms.RadioButton();
            this.INVaccept = new System.Windows.Forms.RadioButton();
            this.INVask = new System.Windows.Forms.RadioButton();
            this.FriendshipOffersGroup = new System.Windows.Forms.GroupBox();
            this.FRdecline = new System.Windows.Forms.RadioButton();
            this.FRaccept = new System.Windows.Forms.RadioButton();
            this.FRask = new System.Windows.Forms.RadioButton();
            this.TeleportLuresGroup.SuspendLayout();
            this.InventoryOffersGroup.SuspendLayout();
            this.FriendshipOffersGroup.SuspendLayout();
            this.SuspendLayout();
            // 
            // FilterScreenshots
            // 
            this.FilterScreenshots.AutoSize = true;
            this.FilterScreenshots.Location = new System.Drawing.Point(1, 16);
            this.FilterScreenshots.Name = "FilterScreenshots";
            this.FilterScreenshots.Size = new System.Drawing.Size(110, 17);
            this.FilterScreenshots.TabIndex = 1;
            this.FilterScreenshots.Text = "Filter Screenshots";
            this.FilterScreenshots.UseVisualStyleBackColor = true;
            // 
            // FilterTyping
            // 
            this.FilterTyping.AutoSize = true;
            this.FilterTyping.Location = new System.Drawing.Point(1, 0);
            this.FilterTyping.Name = "FilterTyping";
            this.FilterTyping.Size = new System.Drawing.Size(83, 17);
            this.FilterTyping.TabIndex = 0;
            this.FilterTyping.Text = "Filter Typing";
            this.FilterTyping.UseVisualStyleBackColor = true;
            // 
            // InstaSit
            // 
            this.InstaSit.AutoSize = true;
            this.InstaSit.Location = new System.Drawing.Point(1, 32);
            this.InstaSit.Name = "InstaSit";
            this.InstaSit.Size = new System.Drawing.Size(64, 17);
            this.InstaSit.TabIndex = 2;
            this.InstaSit.Text = "Insta-Sit";
            this.InstaSit.UseVisualStyleBackColor = true;
            // 
            // TeleportLuresGroup
            // 
            this.TeleportLuresGroup.Controls.Add(this.TPdecline);
            this.TeleportLuresGroup.Controls.Add(this.TPaccept);
            this.TeleportLuresGroup.Controls.Add(this.TPask);
            this.TeleportLuresGroup.Location = new System.Drawing.Point(106, 0);
            this.TeleportLuresGroup.Name = "TeleportLuresGroup";
            this.TeleportLuresGroup.Size = new System.Drawing.Size(159, 30);
            this.TeleportLuresGroup.TabIndex = 3;
            this.TeleportLuresGroup.TabStop = false;
            this.TeleportLuresGroup.Text = "Teleport Lures";
            // 
            // TPdecline
            // 
            this.TPdecline.AutoSize = true;
            this.TPdecline.Location = new System.Drawing.Point(97, 12);
            this.TPdecline.Name = "TPdecline";
            this.TPdecline.Size = new System.Drawing.Size(61, 17);
            this.TPdecline.TabIndex = 2;
            this.TPdecline.Text = "Decline";
            this.TPdecline.UseVisualStyleBackColor = true;
            // 
            // TPaccept
            // 
            this.TPaccept.AutoSize = true;
            this.TPaccept.Location = new System.Drawing.Point(41, 12);
            this.TPaccept.Name = "TPaccept";
            this.TPaccept.Size = new System.Drawing.Size(59, 17);
            this.TPaccept.TabIndex = 1;
            this.TPaccept.Text = "Accept";
            this.TPaccept.UseVisualStyleBackColor = true;
            // 
            // TPask
            // 
            this.TPask.AutoSize = true;
            this.TPask.Checked = true;
            this.TPask.Location = new System.Drawing.Point(3, 12);
            this.TPask.Name = "TPask";
            this.TPask.Size = new System.Drawing.Size(43, 17);
            this.TPask.TabIndex = 0;
            this.TPask.TabStop = true;
            this.TPask.Text = "Ask";
            this.TPask.UseVisualStyleBackColor = true;
            // 
            // InventoryOffersGroup
            // 
            this.InventoryOffersGroup.Controls.Add(this.INVdecline);
            this.InventoryOffersGroup.Controls.Add(this.INVaccept);
            this.InventoryOffersGroup.Controls.Add(this.INVask);
            this.InventoryOffersGroup.Location = new System.Drawing.Point(106, 29);
            this.InventoryOffersGroup.Name = "InventoryOffersGroup";
            this.InventoryOffersGroup.Size = new System.Drawing.Size(159, 30);
            this.InventoryOffersGroup.TabIndex = 4;
            this.InventoryOffersGroup.TabStop = false;
            this.InventoryOffersGroup.Text = "Inventory Offers";
            // 
            // INVdecline
            // 
            this.INVdecline.AutoSize = true;
            this.INVdecline.Location = new System.Drawing.Point(97, 12);
            this.INVdecline.Name = "INVdecline";
            this.INVdecline.Size = new System.Drawing.Size(61, 17);
            this.INVdecline.TabIndex = 2;
            this.INVdecline.Text = "Decline";
            this.INVdecline.UseVisualStyleBackColor = true;
            // 
            // INVaccept
            // 
            this.INVaccept.AutoSize = true;
            this.INVaccept.Enabled = false;
            this.INVaccept.Location = new System.Drawing.Point(41, 12);
            this.INVaccept.Name = "INVaccept";
            this.INVaccept.Size = new System.Drawing.Size(59, 17);
            this.INVaccept.TabIndex = 1;
            this.INVaccept.Text = "Accept";
            this.INVaccept.UseVisualStyleBackColor = true;
            // 
            // INVask
            // 
            this.INVask.AutoSize = true;
            this.INVask.Checked = true;
            this.INVask.Location = new System.Drawing.Point(3, 12);
            this.INVask.Name = "INVask";
            this.INVask.Size = new System.Drawing.Size(43, 17);
            this.INVask.TabIndex = 0;
            this.INVask.TabStop = true;
            this.INVask.Text = "Ask";
            this.INVask.UseVisualStyleBackColor = true;
            // 
            // FriendshipOffersGroup
            // 
            this.FriendshipOffersGroup.Controls.Add(this.FRdecline);
            this.FriendshipOffersGroup.Controls.Add(this.FRaccept);
            this.FriendshipOffersGroup.Controls.Add(this.FRask);
            this.FriendshipOffersGroup.Location = new System.Drawing.Point(106, 58);
            this.FriendshipOffersGroup.Name = "FriendshipOffersGroup";
            this.FriendshipOffersGroup.Size = new System.Drawing.Size(159, 30);
            this.FriendshipOffersGroup.TabIndex = 5;
            this.FriendshipOffersGroup.TabStop = false;
            this.FriendshipOffersGroup.Text = "Friendship Offers";
            // 
            // FRdecline
            // 
            this.FRdecline.AutoSize = true;
            this.FRdecline.Location = new System.Drawing.Point(97, 12);
            this.FRdecline.Name = "FRdecline";
            this.FRdecline.Size = new System.Drawing.Size(61, 17);
            this.FRdecline.TabIndex = 2;
            this.FRdecline.Text = "Decline";
            this.FRdecline.UseVisualStyleBackColor = true;
            // 
            // FRaccept
            // 
            this.FRaccept.AutoSize = true;
            this.FRaccept.Location = new System.Drawing.Point(41, 12);
            this.FRaccept.Name = "FRaccept";
            this.FRaccept.Size = new System.Drawing.Size(59, 17);
            this.FRaccept.TabIndex = 1;
            this.FRaccept.Text = "Accept";
            this.FRaccept.UseVisualStyleBackColor = true;
            // 
            // FRask
            // 
            this.FRask.AutoSize = true;
            this.FRask.Checked = true;
            this.FRask.Location = new System.Drawing.Point(3, 12);
            this.FRask.Name = "FRask";
            this.FRask.Size = new System.Drawing.Size(43, 17);
            this.FRask.TabIndex = 0;
            this.FRask.TabStop = true;
            this.FRask.Text = "Ask";
            this.FRask.UseVisualStyleBackColor = true;
            // 
            // StickyFilters
            // 
            this.Controls.Add(this.TeleportLuresGroup);
            this.Controls.Add(this.InstaSit);
            this.Controls.Add(this.FilterTyping);
            this.Controls.Add(this.FilterScreenshots);
            this.Controls.Add(this.InventoryOffersGroup);
            this.Controls.Add(this.FriendshipOffersGroup);
            this.Name = "StickyFilters";
            this.Size = new System.Drawing.Size(269, 92);
            this.TeleportLuresGroup.ResumeLayout(false);
            this.TeleportLuresGroup.PerformLayout();
            this.InventoryOffersGroup.ResumeLayout(false);
            this.InventoryOffersGroup.PerformLayout();
            this.FriendshipOffersGroup.ResumeLayout(false);
            this.FriendshipOffersGroup.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
    }
}